package com.shxy.rlzy.config;

import com.shxy.rlzy.security.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @program: rlzy
 * @description: security配置
 * @author: tanhao
 * @create: 2021-08-17 22:25
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private LoginSuccessHandler loginSuccessHandler;
    @Autowired
    private LoginFailureHandler loginFailureHandler;
    @Autowired
    private CaptchaFilter captchaFilter;
    @Autowired
    private JwtAuthenticationEntryPoint authenticationEntryPoint;
    @Autowired
    private JwtAccessDeniedHandler accessDeniedHandler;
    @Autowired
    private UserDetailServiceImpl userDetailService;

    @Bean
    JwtAuthenticationFilter jwtAuthenticationFilter() throws Exception {
        JwtAuthenticationFilter jwtAuthenticationFilter = new JwtAuthenticationFilter(authenticationManager());
        return jwtAuthenticationFilter;
    }

    @Bean
    BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    private static final String[] URL_WHITELIST = {
            "/login",
            "/logout",
            "/captcha",
            "/favicon.ico",
            "/test/pass"
    };

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and().csrf().disable()
            // 登录配置
            .formLogin()
                .successHandler(loginSuccessHandler)    // 登录成功处理器
                .failureHandler(loginFailureHandler)    // 登录失败处理器

            // 禁用session
            .and()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS) // 禁用session

            // 配置拦截规则
            .and()
                .authorizeRequests()
                .antMatchers(URL_WHITELIST).permitAll() // 白名单内的访问不拦截
                .anyRequest().authenticated()   // 其他请求全部拦截

            // 异常处理器
            .and()
                .exceptionHandling()
                .authenticationEntryPoint(authenticationEntryPoint) // 未认证异常处理器
                .accessDeniedHandler(accessDeniedHandler)   // 访问被拒绝异常处理器

            // 配置自定义的过滤器
            .and()
                .addFilter(jwtAuthenticationFilter())   // jwt认证过滤器
                .addFilterBefore(captchaFilter, UsernamePasswordAuthenticationFilter.class);    // 检查用户名密码之前先检查验证码

    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailService);
    }
}
